home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Python / modsupport.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-04  |  9.5 KB  |  507 lines

  1. /* Module support implementation */
  2.  
  3. #include "Python.h"
  4.  
  5. #ifdef MPW /* MPW pushes 'extended' for float and double types with varargs */
  6. typedef extended va_double;
  7. #else
  8. typedef double va_double;
  9. #endif
  10.  
  11. /* Package context -- the full module name for package imports */
  12. char *_Py_PackageContext = NULL;
  13.  
  14. /* Py_InitModule4() parameters:
  15.    - name is the module name
  16.    - methods is the list of top-level functions
  17.    - doc is the documentation string
  18.    - passthrough is passed as self to functions defined in the module
  19.    - api_version is the value of PYTHON_API_VERSION at the time the
  20.      module was compiled
  21.  
  22.    Return value is a borrowed reference to the module object; or NULL
  23.    if an error occurred (in Python 1.4 and before, errors were fatal).
  24.    Errors may still leak memory.
  25. */
  26.  
  27. static char api_version_warning[] =
  28. "WARNING: Python C API version mismatch for module %s:\n\
  29.   This Python has API version %d, module %s has version %d.\n";
  30.  
  31. PyObject *
  32. Py_InitModule4(name, methods, doc, passthrough, module_api_version)
  33.     char *name;
  34.     PyMethodDef *methods;
  35.     char *doc;
  36.     PyObject *passthrough;
  37.     int module_api_version;
  38. {
  39.     PyObject *m, *d, *v;
  40.     PyMethodDef *ml;
  41.     if (!Py_IsInitialized())
  42.         Py_FatalError("Interpreter not initialized (version mismatch?)");
  43.     if (module_api_version != PYTHON_API_VERSION)
  44.         fprintf(stderr, api_version_warning,
  45.             name, PYTHON_API_VERSION, name, module_api_version);
  46.     if (_Py_PackageContext != NULL) {
  47.         char *p = strrchr(_Py_PackageContext, '.');
  48.         if (p != NULL && strcmp(name, p+1) == 0) {
  49.             name = _Py_PackageContext;
  50.             _Py_PackageContext = NULL;
  51.         }
  52.     }
  53.     if ((m = PyImport_AddModule(name)) == NULL)
  54.         return NULL;
  55.     d = PyModule_GetDict(m);
  56.     for (ml = methods; ml->ml_name != NULL; ml++) {
  57.         v = PyCFunction_New(ml, passthrough);
  58.         if (v == NULL)
  59.             return NULL;
  60.         if (PyDict_SetItemString(d, ml->ml_name, v) != 0)
  61.             return NULL;
  62.         Py_DECREF(v);
  63.     }
  64.     if (doc != NULL) {
  65.         v = PyString_FromString(doc);
  66.         if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0)
  67.             return NULL;
  68.         Py_DECREF(v);
  69.     }
  70.     return m;
  71. }
  72.  
  73.  
  74. /* Helper for mkvalue() to scan the length of a format */
  75.  
  76. static int countformat Py_PROTO((char *format, int endchar));
  77. static int countformat(format, endchar)
  78.     char *format;
  79.     int endchar;
  80. {
  81.     int count = 0;
  82.     int level = 0;
  83.     while (level > 0 || *format != endchar) {
  84.         switch (*format) {
  85.         case '\0':
  86.             /* Premature end */
  87.             PyErr_SetString(PyExc_SystemError,
  88.                     "unmatched paren in format");
  89.             return -1;
  90.         case '(':
  91.         case '[':
  92.         case '{':
  93.             if (level == 0)
  94.                 count++;
  95.             level++;
  96.             break;
  97.         case ')':
  98.         case ']':
  99.         case '}':
  100.             level--;
  101.             break;
  102.         case '#':
  103.         case '&':
  104.         case ',':
  105.         case ':':
  106.         case ' ':
  107.         case '\t':
  108.             break;
  109.         default:
  110.             if (level == 0)
  111.                 count++;
  112.         }
  113.         format++;
  114.     }
  115.     return count;
  116. }
  117.  
  118.  
  119. /* Generic function to create a value -- the inverse of getargs() */
  120. /* After an original idea and first implementation by Steven Miale */
  121.  
  122. static PyObject *do_mktuple Py_PROTO((char**, va_list *, int, int));
  123. static PyObject *do_mklist Py_PROTO((char**, va_list *, int, int));
  124. static PyObject *do_mkdict Py_PROTO((char**, va_list *, int, int));
  125. static PyObject *do_mkvalue Py_PROTO((char**, va_list *));
  126.  
  127.  
  128. static PyObject *
  129. do_mkdict(p_format, p_va, endchar, n)
  130.     char **p_format;
  131.     va_list *p_va;
  132.     int endchar;
  133.     int n;
  134. {
  135.     PyObject *d;
  136.     int i;
  137.     if (n < 0)
  138.         return NULL;
  139.     if ((d = PyDict_New()) == NULL)
  140.         return NULL;
  141.     for (i = 0; i < n; i+= 2) {
  142.         PyObject *k, *v;
  143.         int err;
  144.         k = do_mkvalue(p_format, p_va);
  145.         if (k == NULL) {
  146.             Py_DECREF(d);
  147.             return NULL;
  148.         }
  149.         v = do_mkvalue(p_format, p_va);
  150.         if (v == NULL) {
  151.             Py_DECREF(k);
  152.             Py_DECREF(d);
  153.             return NULL;
  154.         }
  155.         err = PyDict_SetItem(d, k, v);
  156.         Py_DECREF(k);
  157.         Py_DECREF(v);
  158.         if (err < 0) {
  159.             Py_DECREF(d);
  160.             return NULL;
  161.         }
  162.     }
  163.     if (d != NULL && **p_format != endchar) {
  164.         Py_DECREF(d);
  165.         d = NULL;
  166.         PyErr_SetString(PyExc_SystemError,
  167.                 "Unmatched paren in format");
  168.     }
  169.     else if (endchar)
  170.         ++*p_format;
  171.     return d;
  172. }
  173.  
  174. static PyObject *
  175. do_mklist(p_format, p_va, endchar, n)
  176.     char **p_format;
  177.     va_list *p_va;
  178.     int endchar;
  179.     int n;
  180. {
  181.     PyObject *v;
  182.     int i;
  183.     if (n < 0)
  184.         return NULL;
  185.     if ((v = PyList_New(n)) == NULL)
  186.         return NULL;
  187.     for (i = 0; i < n; i++) {
  188.         PyObject *w = do_mkvalue(p_format, p_va);
  189.         if (w == NULL) {
  190.             Py_DECREF(v);
  191.             return NULL;
  192.         }
  193.         PyList_SetItem(v, i, w);
  194.     }
  195.     if (v != NULL && **p_format != endchar) {
  196.         Py_DECREF(v);
  197.         v = NULL;
  198.         PyErr_SetString(PyExc_SystemError,
  199.                 "Unmatched paren in format");
  200.     }
  201.     else if (endchar)
  202.         ++*p_format;
  203.     return v;
  204. }
  205.  
  206. static int
  207. _ustrlen(Py_UNICODE *u)
  208. {
  209.     int i = 0;
  210.     Py_UNICODE *v = u;
  211.     while (*v != 0) { i++; v++; } 
  212.     return i;
  213. }
  214.  
  215. static PyObject *
  216. do_mktuple(p_format, p_va, endchar, n)
  217.     char **p_format;
  218.     va_list *p_va;
  219.     int endchar;
  220.     int n;
  221. {
  222.     PyObject *v;
  223.     int i;
  224.     if (n < 0)
  225.         return NULL;
  226.     if ((v = PyTuple_New(n)) == NULL)
  227.         return NULL;
  228.     for (i = 0; i < n; i++) {
  229.         PyObject *w = do_mkvalue(p_format, p_va);
  230.         if (w == NULL) {
  231.             Py_DECREF(v);
  232.             return NULL;
  233.         }
  234.         PyTuple_SetItem(v, i, w);
  235.     }
  236.     if (v != NULL && **p_format != endchar) {
  237.         Py_DECREF(v);
  238.         v = NULL;
  239.         PyErr_SetString(PyExc_SystemError,
  240.                 "Unmatched paren in format");
  241.     }
  242.     else if (endchar)
  243.         ++*p_format;
  244.     return v;
  245. }
  246.  
  247. static PyObject *
  248. do_mkvalue(p_format, p_va)
  249.     char **p_format;
  250.     va_list *p_va;
  251. {
  252.     for (;;) {
  253.         switch (*(*p_format)++) {
  254.         case '(':
  255.             return do_mktuple(p_format, p_va, ')',
  256.                       countformat(*p_format, ')'));
  257.  
  258.         case '[':
  259.             return do_mklist(p_format, p_va, ']',
  260.                      countformat(*p_format, ']'));
  261.  
  262.         case '{':
  263.             return do_mkdict(p_format, p_va, '}',
  264.                      countformat(*p_format, '}'));
  265.  
  266.         case 'b':
  267.         case 'h':
  268.         case 'i':
  269.             return PyInt_FromLong((long)va_arg(*p_va, int));
  270.  
  271.         case 'l':
  272.             return PyInt_FromLong((long)va_arg(*p_va, long));
  273.  
  274. #ifdef HAVE_LONG_LONG
  275.         case 'L':
  276.             return PyLong_FromLongLong((LONG_LONG)va_arg(*p_va, LONG_LONG));
  277. #endif
  278.         case 'u':
  279.         {
  280.             PyObject *v;
  281.             Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *);
  282.             int n;
  283.             if (**p_format == '#') {
  284.                 ++*p_format;
  285.                 n = va_arg(*p_va, int);
  286.             }
  287.             else
  288.                 n = -1;
  289.             if (u == NULL) {
  290.                 v = Py_None;
  291.                 Py_INCREF(v);
  292.             }
  293.             else {
  294.                 if (n < 0)
  295.                     n = _ustrlen(u);
  296.                 v = PyUnicode_FromUnicode(u, n);
  297.             }
  298.             return v;
  299.         }
  300.         case 'f':
  301.         case 'd':
  302.             return PyFloat_FromDouble(
  303.                 (double)va_arg(*p_va, va_double));
  304.  
  305.         case 'c':
  306.         {
  307.             char p[1];
  308.             p[0] = va_arg(*p_va, int);
  309.             return PyString_FromStringAndSize(p, 1);
  310.         }
  311.  
  312.         case 's':
  313.         case 'z':
  314.         {
  315.             PyObject *v;
  316.             char *str = va_arg(*p_va, char *);
  317.             int n;
  318.             if (**p_format == '#') {
  319.                 ++*p_format;
  320.                 n = va_arg(*p_va, int);
  321.             }
  322.             else
  323.                 n = -1;
  324.             if (str == NULL) {
  325.                 v = Py_None;
  326.                 Py_INCREF(v);
  327.             }
  328.             else {
  329.                 if (n < 0)
  330.                     n = strlen(str);
  331.                 v = PyString_FromStringAndSize(str, n);
  332.             }
  333.             return v;
  334.         }
  335.  
  336.         case 'N':
  337.         case 'S':
  338.         case 'O':
  339.         if (**p_format == '&') {
  340.             typedef PyObject *(*converter) Py_PROTO((void *));
  341.             converter func = va_arg(*p_va, converter);
  342.             void *arg = va_arg(*p_va, void *);
  343.             ++*p_format;
  344.             return (*func)(arg);
  345.         }
  346.         else {
  347.             PyObject *v;
  348.             v = va_arg(*p_va, PyObject *);
  349.             if (v != NULL) {
  350.                 if (*(*p_format - 1) != 'N')
  351.                     Py_INCREF(v);
  352.             }
  353.             else if (!PyErr_Occurred())
  354.                 /* If a NULL was passed
  355.                  * because a call that should
  356.                  * have constructed a value
  357.                  * failed, that's OK, and we
  358.                  * pass the error on; but if
  359.                  * no error occurred it's not
  360.                  * clear that the caller knew
  361.                  * what she was doing. */
  362.                 PyErr_SetString(PyExc_SystemError,
  363.                     "NULL object passed to Py_BuildValue");
  364.             return v;
  365.         }
  366.  
  367.         case ':':
  368.         case ',':
  369.         case ' ':
  370.         case '\t':
  371.             break;
  372.  
  373.         default:
  374.             PyErr_SetString(PyExc_SystemError,
  375.                 "bad format char passed to Py_BuildValue");
  376.             return NULL;
  377.  
  378.         }
  379.     }
  380. }
  381.  
  382.  
  383. #ifdef HAVE_STDARG_PROTOTYPES
  384. /* VARARGS 2 */
  385. PyObject *Py_BuildValue(char *format, ...)
  386. #else
  387. /* VARARGS */
  388. PyObject *Py_BuildValue(va_alist) va_dcl
  389. #endif
  390. {
  391.     va_list va;
  392.     PyObject* retval;
  393. #ifdef HAVE_STDARG_PROTOTYPES
  394.     va_start(va, format);
  395. #else
  396.     char *format;
  397.     va_start(va);
  398.     format = va_arg(va, char *);
  399. #endif
  400.     retval = Py_VaBuildValue(format, va);
  401.     va_end(va);
  402.     return retval;
  403. }
  404.  
  405. PyObject *
  406. Py_VaBuildValue(format, va)
  407.     char *format;
  408.     va_list va;
  409. {
  410.     char *f = format;
  411.     int n = countformat(f, '\0');
  412.     va_list lva;
  413.  
  414. #ifdef VA_LIST_IS_ARRAY
  415.     memcpy(lva, va, sizeof(va_list));
  416. #else
  417.     lva = va;
  418. #endif
  419.  
  420.     if (n < 0)
  421.         return NULL;
  422.     if (n == 0) {
  423.         Py_INCREF(Py_None);
  424.         return Py_None;
  425.     }
  426.     if (n == 1)
  427.         return do_mkvalue(&f, &lva);
  428.     return do_mktuple(&f, &lva, '\0', n);
  429. }
  430.  
  431.  
  432. #ifdef HAVE_STDARG_PROTOTYPES
  433. PyObject *
  434. PyEval_CallFunction(PyObject *obj, char *format, ...)
  435. #else
  436. PyObject *
  437. PyEval_CallFunction(obj, format, va_alist)
  438.     PyObject *obj;
  439.     char *format;
  440.     va_dcl
  441. #endif
  442. {
  443.     va_list vargs;
  444.     PyObject *args;
  445.     PyObject *res;
  446.  
  447. #ifdef HAVE_STDARG_PROTOTYPES
  448.     va_start(vargs, format);
  449. #else
  450.     va_start(vargs);
  451. #endif
  452.  
  453.     args = Py_VaBuildValue(format, vargs);
  454.     va_end(vargs);
  455.  
  456.     if (args == NULL)
  457.         return NULL;
  458.  
  459.     res = PyEval_CallObject(obj, args);
  460.     Py_DECREF(args);
  461.  
  462.     return res;
  463. }
  464.  
  465.  
  466. #ifdef HAVE_STDARG_PROTOTYPES
  467. PyObject *
  468. PyEval_CallMethod(PyObject *obj, char *methodname, char *format, ...)
  469. #else
  470. PyObject *
  471. PyEval_CallMethod(obj, methodname, format, va_alist)
  472.     PyObject *obj;
  473.     char *methodname;
  474.     char *format;
  475.     va_dcl
  476. #endif
  477. {
  478.     va_list vargs;
  479.     PyObject *meth;
  480.     PyObject *args;
  481.     PyObject *res;
  482.  
  483.     meth = PyObject_GetAttrString(obj, methodname);
  484.     if (meth == NULL)
  485.         return NULL;
  486.  
  487. #ifdef HAVE_STDARG_PROTOTYPES
  488.     va_start(vargs, format);
  489. #else
  490.     va_start(vargs);
  491. #endif
  492.  
  493.     args = Py_VaBuildValue(format, vargs);
  494.     va_end(vargs);
  495.  
  496.     if (args == NULL) {
  497.         Py_DECREF(meth);
  498.         return NULL;
  499.     }
  500.  
  501.     res = PyEval_CallObject(meth, args);
  502.     Py_DECREF(meth);
  503.     Py_DECREF(args);
  504.  
  505.     return res;
  506. }
  507.